package com.ruoyi.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.user.domain.UserHelpGroup;
import com.ruoyi.user.domain.bo.UserHelpGroupBo;
import com.ruoyi.user.domain.vo.UserHelpGroupVo;
import com.ruoyi.user.mapper.UserHelpGroupMapper;
import com.ruoyi.user.service.IUserHelpGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 帮扶小组Service业务层处理
 *
 * @author strongme
 * @date 2023-01-26
 */
@RequiredArgsConstructor
@Service
public class UserHelpGroupServiceImpl implements IUserHelpGroupService {

    private final UserHelpGroupMapper baseMapper;

    /**
     * 查询帮扶小组
     */
    @Override
    public UserHelpGroupVo queryById(Long helpGroupId) {
        return baseMapper.selectVoById(helpGroupId);
    }

    @Override
    public List<UserHelpGroupVo> queryByUserId(Long userId) {
        return baseMapper.selectVoList(Wrappers.<UserHelpGroup>lambdaQuery().eq(UserHelpGroup::getUserId, userId));
    }

    /**
     * 查询帮扶小组列表
     */
    @Override
    public TableDataInfo<UserHelpGroupVo> queryPageList(UserHelpGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserHelpGroup> lqw = buildQueryWrapper(bo);
        Page<UserHelpGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询帮扶小组列表
     */
    @Override
    public List<UserHelpGroupVo> queryList(UserHelpGroupBo bo) {
        LambdaQueryWrapper<UserHelpGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserHelpGroup> buildQueryWrapper(UserHelpGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserHelpGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserHelpGroup::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), UserHelpGroup::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getIdno()), UserHelpGroup::getIdno, bo.getIdno());
        lqw.eq(StringUtils.isNotBlank(bo.getLxdh()), UserHelpGroup::getLxdh, bo.getLxdh());
        lqw.eq(StringUtils.isNotBlank(bo.getGzdw()), UserHelpGroup::getGzdw, bo.getGzdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGzzw()), UserHelpGroup::getGzzw, bo.getGzzw());
        lqw.eq(StringUtils.isNotBlank(bo.getSssf()), UserHelpGroup::getSssf, bo.getSssf());
        lqw.eq(StringUtils.isNotBlank(bo.getLxdz()), UserHelpGroup::getLxdz, bo.getLxdz());
        return lqw;
    }

    /**
     * 新增帮扶小组
     */
    @Override
    public Boolean insertByBo(UserHelpGroupBo bo) {
        UserHelpGroup add = BeanUtil.toBean(bo, UserHelpGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setHelpGroupId(add.getHelpGroupId());
        }
        return flag;
    }

    /**
     * 修改帮扶小组
     */
    @Override
    public Boolean updateByBo(UserHelpGroupBo bo) {
        UserHelpGroup update = BeanUtil.toBean(bo, UserHelpGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserHelpGroup entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除帮扶小组
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
